package com.kafka.valid;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import com.kafka.bean.Device;
import com.kafka.common.String.StringValues;
import com.kafka.core.BaseClass;
import com.kafka.enumtype.ResponseType;
import com.kafka.util.CheckUtil;

public class DeviceValid extends BaseClass{
	/**
	 * 绑定腕表校验
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validBind(JSONObject params,String msgId) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		// 绑定设备或解绑设备
		try {
			//**************************必填字段校验*****************************************
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("dtype").isEmpty()){
				return failMsg(result,StringValues.DTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("dname").isEmpty()){
				return failMsg(result,StringValues.DNAME_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("dcode").isEmpty()){
				return failMsg(result,StringValues.DCODE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("model").isEmpty()){
				return failMsg(result,StringValues.MODEL_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("manu").isEmpty()){
				return failMsg(result,StringValues.MANU_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("producedt").isEmpty()){
				return failMsg(result,StringValues.PRODUCEDT_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			//**************************查询字段校验*****************************************
			query.clear();
			query.put("uuid", params.getString("uuid"));
			if (userDao.selectByCondition(query) == null) {
				return failMsg(result,StringValues.UUID_NOT_FOUND,ResponseType.UUID_EXIST.toString(),msgId);
			}
			
			query.clear();
			query = new HashMap<>();
			query.put("dcode", params.getString("dcode"));
			Device device = deviceDao.selectByCondition(query);
			if (device != null) {
				return failMsg(result,StringValues.DEVICE_EXIST,ResponseType.DEVICE_EXIST.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			if(!CheckUtil.isNumber(params.getString("dtype")) && !params.getString("dtype").isEmpty()){
				return failMsg(result,StringValues.DTYPE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isDate(params.getString("producedt")) && !params.getString("producedt").isEmpty()){
				return failMsg(result,StringValues.PRODUCEDT_FORMAT_ERROR,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
		return null;
	}
	
	/**
	 * 解绑腕表校验
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validUnbind(JSONObject params, String msgId) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		// 绑定设备或解绑设备
		try {
			//**************************必填字段校验*****************************************
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("dtype").isEmpty()){
				return failMsg(result,StringValues.DTYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("dname").isEmpty()){
				return failMsg(result,StringValues.DNAME_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("dcode").isEmpty()){
				return failMsg(result,StringValues.DCODE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("model").isEmpty()){
				return failMsg(result,StringValues.MODEL_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("manu").isEmpty()){
				return failMsg(result,StringValues.MANU_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("producedt").isEmpty()){
				return failMsg(result,StringValues.PRODUCEDT_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			//**************************查询字段校验*****************************************
			query.clear();
			query.put("uuid", params.getString("uuid"));
			if (userDao.selectByCondition(query) == null) {
				return failMsg(result,StringValues.UUID_NOT_FOUND,ResponseType.UUID_EXIST.toString(),msgId);
			}
			
			query = new HashMap<>();
			query.put("dcode", params.getString("dcode"));
			if (deviceDao.selectByCondition(query) == null) {
				return failMsg(result,StringValues.DEVICE_NOT_EXIST,ResponseType.DEVICE_NOT_FOUND.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			if(!CheckUtil.isNumber(params.getString("dtype")) && !params.getString("dtype").isEmpty()){
				return failMsg(result,StringValues.DTYPE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isDate(params.getString("producedt")) && !params.getString("producedt").isEmpty()){
				return failMsg(result,StringValues.PRODUCEDT_FORMAT_ERROR,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
		return null;
	}


}
